<template>
  <div class="app-container">

    <!-- 表格-条件筛区域  -->
    <div class="filter-container">
     <!-- <el-input
        v-model="searchParam.SEARCH_LIKE_name"
        placeholder="请输入关键字"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">搜索 </el-button>-->
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">新增 </el-button>
    </div>

    <!-- 表格-内容区域 -->
    <el-table :key="tableKey" v-loading="listLoading" :data="tableList" border fit highlight-current-row style="width: 100%;height: 250px">
      <el-table-column
        v-for="column in columns"
        :key="column.prop"
        :prop="column.prop"
        :label="column.label"
        :width="column.width"
        :align="column.align?column.align:'center'"
        :min-width="column.minWidth"
        :sortable="column.sortable"
        :fixed="column.fixed"
        :type="column.type"
        :formatter="column.formatter"
      >
        <template slot-scope="scope">
          <img v-if="column.imgFlag==true" :src="CMBASE.baseFileContext + scope.row[column.prop]" width="60" height="60" class="img">
          <!-- 行内显示开关按钮 -->
          <el-switch v-else-if="column.switchFlag==true" v-model="scope.row[column.prop]" :active-value="1" :inactive-value="0" disabled />
          <span v-else-if="column.formatter"> {{ column.formatter(scope.row) }}</span>
          <span v-else>{{ scope.row[column.prop] }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" width="290" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="info" style="width:90px" @click="viewSpecGroupKeyList(scope.row.id,'')">参数值列表</el-button>
          <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑 </el-button>
          <el-button type="danger" size="mini" @click="handleDelete(scope.row)">删除 </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 表格-页脚区域 -->
    <pagination
      v-show="tableTotal>0"
      :total="tableTotal"
      :page.sync="pageRequest.pageNum"
      :limit.sync="pageRequest.pageSize"
      @pagination="getList"
    />

    <!--新增编辑参数组界面-->
    <el-dialog :title="operation?'新增参数组':'编辑参数组'" :visible.sync="dialogFormVisible" :modal-append-to-body="false" :modal="false" :close-on-click-modal="false" width="45%" top="15vh" >
      <div>
        <el-form ref="dataForm" :model="dataForm" :rules="dataFormRules" label-width="140px" style="width: 80%">
          <el-form-item label="商品分类" prop="classifyId">
            <el-cascader ref="classCascader" v-model="dataForm.classifyId" :options="treeTableDataList" :props="props" collapse-tags clearable separator=" " @change="handleFilterChangeType" disabled/>
          </el-form-item>
          <el-form-item label="参数组名称" prop="name">
            <el-input v-model="dataForm.name" auto-complete="off" />
          </el-form-item>
          <el-form-item label="参数组编码" prop="code">
            <el-input v-model="dataForm.code" auto-complete="off" />
          </el-form-item>
          <el-form-item label="排序" prop="orderNo">
            <el-input v-model="dataForm.orderNo" auto-complete="off" />
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="dataForm.remark" auto-complete="off" />
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click.native="closeDialog()">取消</el-button>
        <el-button :loading="editLoading" size="mini" type="primary" @click.native="saveFormData()">提交
        </el-button>
      </div>
    </el-dialog>

    <!--spu参数组key列表弹窗-->
    <el-dialog title="参数Key列表" width="60%" top="10vh" :visible.sync="getKeyListVisible" :modal-append-to-body="false"  :modal="false" :close-on-click-modal="false">
      <div v-if="getKeyListVisible" style="height:430px">
        <spec-groupkey ref="specGroupkey" :row-group-id="rowGroupId" style="padding-top: 0px"></spec-groupkey>
      </div>
    </el-dialog>
  </div>
</template>
<script>

import waves from '@/directive/waves' // Waves directive
import Pagination from '@/components/Pagination/index' // Secondary package based on el-pagination
import { getMSpecGrpList, saveMSpecGrpInfo, deleteMSpecGrpInfo, getClassifyList } from '@/api/modules/goodsInfo'
import specGroupkey from '@/views/goodsattr/specGroupKey'

export default {
  name: 'ProductBrand',
  components: { Pagination, specGroupkey },
  props: {
    rowClassifyId: {
      type: [String],
      default: ''
    }
  },
  directives: { waves },
  filters: {},
  data() {
    return {
      tableKey: 0,
      tableList: [], // 表格数据源
      tableTotal: 0, // 表格总条数数据
      treeTableDataList: [],
      listLoading: true,
      pageRequest: { pageNum: 1, pageSize: 10 },
      searchParam: { SEARCH_LIKE_name: '' },
      searchParamKey: { SEARCH_LIKE_spec_group_id: '', SEARCH_LIKE_spec_name: '' },

      dialogFormVisible: false, // 默认不显示弹出框
      operation: true, // true:新增, false:编辑
      editLoading: false, // 保存表单时loading框

      tableListKey: [], // 参数组key表格数据源
      listLoadingKey: true,
      treeTableDataListKey: [],
      getKeyListVisible: false, // 默认不显示参数值弹出框

      columns: [
        // { prop: 'id', label: 'id', sortable: false },
        // { prop: 'classifyName', label: '商品分类', sortable: false },
        { prop: 'name', label: '参数组名称', sortable: false },
        { prop: 'code', label: '参数组编码', sortable: false, minWidth: 40 },
        { prop: 'orderNo', label: '排序', sortable: false, minWidth: 40 },
        // { prop: 'remark', label: '备注', sortable: false },
        { prop: 'createTime', label: '创建时间', sortable: false, minWidth: 45 }
      ],
      fileList: [],
      // 新增编辑界面数据
      dataForm: {},
      // 表单检验规则
      dataFormRules: {
        classifyId: [{ required: true, message: '请选择商品分类', trigger: 'blur' }],
        name: [{ required: true, message: '请输入参数组名称', trigger: 'blur' }],
        code: [{ required: true, message: '请输入参数组编码', trigger: 'blur' }],
        orderNo: [{ required: true, message: '请输入排序', trigger: 'blur' }],
        remark: [{ required: false, message: '请输入备注', trigger: 'blur' }]
      },
      operationKey: true,
      dialogFormVisibleKey: false,
      editLoadingKey: false, // 保存表单时loading框
      props: {
        multiple: false, // 是否多选
        label: 'name',
        value: 'id',
        children: 'children'
      },
      rowGroupId: ''
    }
  },
  created() {
    this.getList()
    this.getClassifyTreeList()
  },
  methods: {
    // 获取表格数据
    getList() {
      this.listLoading = true
      // 【API请求】----获取用户列表数据
      this.searchParam.SEARCH_LIKE_classify_id = this.rowClassifyId
      this.pageRequest.searchParam = this.searchParam
      getMSpecGrpList(this.pageRequest).then(response => {
        console.log(response)
        this.tableList = response.result.content
        this.tableTotal = response.result.total
        this.listLoading = false
      }).catch((res) => {
        this.$message({ message: '列表获取失败', type: 'error' })
      })
    },
    // 获取分类数据
    getClassifyTreeList() {
      // 【API请求】----获取分类列表数据
      getClassifyList().then(response => {
        this.treeTableDataList = response.result.content
      }).catch(() => {
      })
    },
    // 商品类型
    handleFilterChangeType(value) {
      console.log(value)
      this.dataForm.classifyId = value[value.length - 1]
      const classLabel = this.$refs['classCascader'].getCheckedNodes()[0].pathLabels
      this.dataForm.classifyName = classLabel[classLabel.length - 1]
    },
    // 表格数据筛选
    handleFilter() {
      this.pageRequest.searchParam = this.searchParam
      this.getList()
    },
    // 【按钮点击】--- 弹出新增表单
    handleCreate() {
      this.operation = true
      this.initDataForm()
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
        this.editLoading = false
      })
    },
    // 【按钮点击】--- 弹出编辑表单
    handleUpdate(row) {
      this.operation = false
      this.dataForm = Object.assign({}, row)

      // 显示编辑菜单
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
        this.editLoading = false
      })
    },
    // 保存表单数据
    saveFormData() {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.editLoading = true
        // 获取分类名称
        const nodesObj = this.$refs['classCascader'].getCheckedNodes()
        this.dataForm.classifyName = nodesObj[0].label
        // 【API请求】----保存数据
        console.log(this.dataForm)
        saveMSpecGrpInfo(this.dataForm).then(() => {
          this.editLoading = false// 关闭loading框
          this.dialogFormVisible = false// 关闭表单页面
          this.initDataForm()// 重置表单数据
          this.getList()// 重新获取表格数据
          this.$notify({ title: '成功', message: '保存成功', type: 'success', duration: 2000 })
        })
      })
    },
    // 【按钮点击】---删除行
    handleDelete(row) {
      this.$confirm('确认删除该记录吗？', '提示', {}).then(() => {
        // 【API请求】---- 删除数据
        deleteMSpecGrpInfo({ ids: row.id }).then(() => {
          this.$message({ message: '操作成功', type: 'success' })
          this.getList()
        })
      }).catch(() => {
      })
    },
    viewSpecGroupKeyList(id, value) {
      this.getKeyListVisible = true
      this.rowGroupId = id
    },

    // 属性值表格数据筛选
    handleFilterKey() {
      var id = this.searchParamKey.SEARCH_LIKE_spec_group_id
      var value = this.searchParamKey.SEARCH_LIKE_spec_name
      this.viewSpecGroupKeyList(id, value)
    },

    // 初始化表单数据，重新渲染
    initDataForm() {
      this.dataForm = {
        id: '',
        classifyId: this.rowClassifyId,
        classifyName: '',
        name: '',
        code: '',
        orderNo: '',
        remark: ''
      }
      this.fileList = []
    },
    // 关闭表单弹出框
    closeDialog() {
      this.$refs['dataForm'].resetFields()
      this.dialogFormVisible = false
    }
  }
}
</script>
